﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class EnterpriseAttributeRepository : BaseRepository
    {
        public int AddAttribute(string enterpriseName)
        {
            int enterpriseId = -1;

            try
            {
                Enterprise enterprise = new Enterprise();
                enterprise.Name = enterpriseName;
                enterprise.adt_DateCreated = DateTime.Now;
                _context.Enterprises.Add(enterprise);
                _context.SaveChanges();
                enterpriseId = enterprise.EnterpriseId;
            }
            catch (Exception ex)
            {
                enterpriseId = -1;
            }

            return enterpriseId;
        }

        public bool RemoveAttribute(string name)
        {
            bool bResult = true;

            try
            {
                Enterprise enterprise = _context.Enterprises.Where(p => p.Name == name).SingleOrDefault();
                if (null != enterprise)
                {
                    _context.Enterprises.Remove(enterprise);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public string GetEnterpriseAttribute(int enterpriseId,string key)
        {
            key = key.Trim();

            string attribute = _context.EnterpriseAttributes.Where(p => p.EnterpriseId == enterpriseId && p.Key.Equals(key)).Select(x=>x.Value).SingleOrDefault();
            return attribute;

        }

    }
}
